//
// Description: 58. 最后一个单词的长度
// Created by Loading on 2021/9/21.
//

#include <bits/stdc++.h>

using namespace std;

int lengthOfLastWord(string s) {
    int len = 0;

    for (int i = (int) s.size() - 1; i >= 0; --i) {
        if (s[i] != ' ') {
            ++len;
        } else {
            // 需过滤末尾空格
            if (len > 0) {
                return len;
            }
        }
    }

    return len;
}

int main() {
    string s = "   fly me   to   the moon  ";
    int len = lengthOfLastWord(s);
    cout << len << endl;

    return 0;
}